//
//  TMRootViewControllers.m
//  Tourism
//
//  Created by zhikuiyu on 15/11/5.
//  Copyright © 2015年 zhikuiyu. All rights reserved.
//

#import "TMRootViewControllers.h"
#import "TBNatioinView.h"
#import "TBNavBottomView.h"
#import "ZYConsultView.h"
#import "ZYFormView.h"
#import "ZYGuideView.h"
#import "ZYAddView.h"
#import "ZYUserView.h"
#import "ZYRecordView.h"

typedef NS_ENUM(int,ViewType){
    ViewTypeAdd = 0,//添加
    ViewTypeConsult = 1,//咨询
    ViewTypeForm = 2, //图标
    ViewTypeGuide = 3
};


@interface TMRootViewControllers ()<TBNatioinViewDelegate,TBNavBottomViewDelegate>
{
    UIView * currentView;
    UIScrollView * addScrollView;
    UIView * detailView;
    NSTimer *timeNow;
    UITextField * currentTF;
    UITextField * continueTF;
}

@property (weak, nonatomic) IBOutlet TBNatioinView *navtionView;
@property (weak, nonatomic) IBOutlet TBNavBottomView *navBottomView;
@property (weak, nonatomic) IBOutlet UIView *backgroView;

@end

@implementation TMRootViewControllers

- (void)viewDidLoad {
    [super viewDidLoad];
    _navtionView.delegate = self;
    
    _navBottomView.delegate = self;
    [_navBottomView addView];
    [_navBottomView twoView];
    [_navBottomView thirdView];
    [_navBottomView fourView];
    self.navBottomView.backgroundColor = ZYColor;
    self.navtionView.backgroundColor = ZYColor;
    
    [_navBottomView.addView setTitle:@"添加" forState:0];
    [_navBottomView.addView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_navBottomView.addView setImage:[UIImage imageNamed:@"itemSelected1"] forState:UIControlStateNormal];
    [_navBottomView.addView setImage:[UIImage imageNamed:@"item1"] forState:UIControlStateSelected];
    
    
    [_navBottomView.twoView setTitle:@"报表" forState:0];
    [_navBottomView.twoView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_navBottomView.twoView setImage:[UIImage imageNamed:@"itemSelected2"] forState:UIControlStateNormal];
    [_navBottomView.twoView setImage:[UIImage imageNamed:@"item2"] forState:UIControlStateSelected];
    
    [_navBottomView.thirdView setTitle:@"指南" forState:0];
    [_navBottomView.thirdView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_navBottomView.thirdView setImage:[UIImage imageNamed:@"itemSelected3"] forState:UIControlStateNormal];
    [_navBottomView.thirdView setImage:[UIImage imageNamed:@"item3"] forState:UIControlStateSelected];
    
    [_navBottomView.fourView setTitle:@"咨询" forState:0];
    [_navBottomView.fourView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_navBottomView.fourView setImage:[UIImage imageNamed:@"itemSelected4"] forState:UIControlStateNormal];
    [_navBottomView.fourView setImage:[UIImage imageNamed:@"item4"] forState:UIControlStateSelected];
    //把_navBottomView放到最上面 不然会覆盖
    [self.view bringSubviewToFront:_navBottomView];
    [self loadView:ViewTypeAdd];
    
}

- (void)loadView:(ViewType)type{
    [currentView removeFromSuperview];
    switch (type) {
        case ViewTypeAdd:
            [_navtionView leftBtn];
            [_navtionView rightBtn];
            currentView = [[ZYAddView alloc]initWithFrame:CGRectMake(0, 0,self.backgroView.width, self.backgroView.height)];
            _navtionView.title = @"添加";
            break;
        case ViewTypeForm:
            currentView = [[ZYFormView alloc]initWithFrame:CGRectMake(0, 0,self.backgroView.width, self.backgroView.height)];
            _navtionView.title = @"图标";
            break;
        case ViewTypeGuide:
            currentView = [[ZYGuideView alloc]initWithFrame:CGRectMake(0, 0,self.backgroView.width, self.backgroView.height)];
            _navtionView.title = @"指南";
            break;
        case ViewTypeConsult:
            currentView = [[ZYConsultView alloc]initWithFrame:CGRectMake(0, 0,self.backgroView.width, self.backgroView.height)];
            _navtionView.title = @"咨询";
            break;
            
        default:
            break;
    }
    if (currentView) {
        [self.backgroView addSubview:currentView];
    }
}
#pragma mark - TBNatioinViewDelegate

- (void)navtionLeftBtnAction:(UIButton *)button{
    detailView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    detailView.backgroundColor = ZYColor;
    [self.view addSubview:detailView];
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 30, 100, 50);
    btn.backgroundColor = [UIColor redColor];
    [btn setTitle:@"返回" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(backBtnIsClick:) forControlEvents:UIControlEventTouchUpInside];
    [detailView addSubview:btn];
    
    UIImageView * imageView = [[UIImageView alloc]init];
    imageView.frame = CGRectMake(0, 80, SCREEN_WIDTH, 220);
    imageView.image = [UIImage imageNamed:@"插画1"];
    [detailView addSubview:imageView];
    
    CGFloat textY = 310;
    CGFloat textH = 50;
    CGFloat padding = 10;
    //获取当前时间
    currentTF = [[UITextField alloc]init];
    currentTF.frame = CGRectMake(10, textY, SCREEN_WIDTH - 20, textH);
    currentTF.backgroundColor = [UIColor whiteColor];
    currentTF.textAlignment = NSTextAlignmentCenter;
    [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(timerFunc) userInfo:nil repeats:YES];
    [detailView addSubview:currentTF];
    //获取持续时间
    continueTF = [[UITextField alloc]init];
    continueTF.frame = CGRectMake(10, textY + (textH + padding), SCREEN_WIDTH - 20, textH);
    continueTF.backgroundColor = [UIColor whiteColor];
    continueTF.textAlignment = NSTextAlignmentCenter;
    [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(timerFunc1) userInfo:nil repeats:YES];
    [detailView addSubview:continueTF];
    
    
    UITextField * stepTF = [[UITextField alloc]init];
    stepTF.frame = CGRectMake(10, textY + 2 * (textH + padding), SCREEN_WIDTH - 20, textH);
    stepTF.backgroundColor = [UIColor whiteColor];
    stepTF.textAlignment = NSTextAlignmentCenter;
    NSString * stepStr = [NSString stringWithFormat:@"步数："];
    stepTF.text = stepStr;
    [stepTF reloadInputViews];
    [detailView addSubview:stepTF];
    
    CGFloat startW = 100;
    CGFloat startH = 100;
    CGFloat startY = 520;
    CGFloat startX = (SCREEN_WIDTH - startW) * 0.5;
    UIButton * startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    startBtn.frame = CGRectMake(startX, startY, startW, startH);
    [startBtn setBackgroundImage:[UIImage imageNamed:@"go"] forState:UIControlStateNormal];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"go－按下"] forState:UIControlStateHighlighted];
    [startBtn addTarget:self action:@selector(startBtnIsClick) forControlEvents:UIControlEventTouchUpInside];
    [detailView addSubview:startBtn];
}
- (void)timerFunc{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *timestamp = [formatter stringFromDate:[NSDate date]];
    NSString * curTime = [NSString stringWithFormat:@"当前时间：%@",timestamp];
    [currentTF setText:curTime];//时间在变化的语句
}
- (void)timerFunc1{
    NSString *timeStr = @"2016-01-01 11:36:00";
    
    NSDateFormatter *date=[[NSDateFormatter alloc] init];
    [date setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSDate *d=[date dateFromString:timeStr];
    NSTimeInterval late=[d timeIntervalSince1970]*1;
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval now=[dat timeIntervalSince1970]*1;
    NSString *timeString=@"";
    
    NSTimeInterval cha=now-late;
    
    if (cha/3600<1) {
        timeString = [NSString stringWithFormat:@"%f", cha/60];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"%@分钟前", timeString];
    }
    if (cha/3600>1&&cha/86400<1) {
        NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
        [dateformatter setDateFormat:@"HH:mm"];
        timeString = [NSString stringWithFormat:@"今天 %@",[dateformatter stringFromDate:d]];
    }
    if (cha/86400>1){
        NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
        [dateformatter setDateFormat:@"HH:mm"];
        timeString = [NSString stringWithFormat:@"持续时间：%@",[dateformatter stringFromDate:d]];
    }
    [continueTF setText:timeString];
}
- (void)startBtnIsClick{
    [self timerFunc1];
}

- (void) backBtnIsClick:(UIButton *)btn{
    [detailView removeFromSuperview];
}

//这个是左边的按钮（写反了）
- (void)navtionrightBtnAction:(UIButton *)button{
    button.selected = !button.selected;
    if (button.selected) {
        [self showUserMessage];
        
    }else{
        self.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    }
}
- (void)showUserMessage{
    self.view.frame = CGRectMake(200, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    self.view.window.backgroundColor = [UIColor whiteColor];
    
    ZYUserView * user = [[ZYUserView alloc]initWithFrame:CGRectMake(-200, 20, 200, SCREEN_HEIGHT)];
    user.backgroundColor = [UIColor redColor];
    [user userTableView];
    [self.view addSubview:user];
    [user bringSubviewToFront:self.view];
    
}


#pragma mark -

- (void)navBottomAddBtnDidClick:(id)sender{
    [self loadView:ViewTypeAdd];
}
- (void)navBottomConsultBtnDidClick:(id)sender{
    [self loadView:ViewTypeConsult];
}
- (void)navBottomFormBtnDidClick:(id)sender{
    [self loadView:ViewTypeForm];
}
- (void)navBottomGuideBtnDidClick:(id)sender{
    [self loadView:ViewTypeGuide];
}

@end
